/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadNivel.java
 *
 * Created on 12/11/2011, 13:53:22
 */
package visao;

import Nivel.Nivel;
import Nivel.NivelNaoCadastradoException;
import fachada.FachadaBDR;
import java.awt.Color;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import subSetor.SubSetor;
import subSetor.SubSetorNaoCadastradoException;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;
import util.KeyValue;

/**
 *
 * @author Administrador
 */
public class CadNivel extends javax.swing.JFrame {

    /** Creates new form CadNivel */
    public CadNivel() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/visao.gif")).getImage());
    }

    /** Creates new form CadNivel */
    public CadNivel(int idNivel) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/visao.gif")).getImage());
        
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            // obtendo a categoria
            Nivel objNivel = objFachadaBDR.procurarNivel(idNivel);
            SubSetor objSubSetor = objNivel.getSubSetor();
            // Setando os valores do objeto Nivel
            id_nivel.setText(String.valueOf(objNivel.getIdNivel()));
            jTextField_Descricao.setText(objNivel.getDescricaoNivel());
            jCheckBox_Status.setSelected(objNivel.getStatusNivel());
            jComboBox_SubSetor.setSelectedItem(new KeyValue(objSubSetor.getDescricaoSubSetor(), Integer.toString(objSubSetor.getIdSubSetor())));

        } catch (NivelNaoCadastradoException ex) {
            Logger.getLogger(CadNivel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CamposObrigatoriosException ex) {
            Logger.getLogger(CadCategoria.class.getName()).log(Level.SEVERE, null, ex);
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        } catch (ErroAcessoRepositorioException ex) {
            Logger.getLogger(CadCategoria.class.getName()).log(Level.SEVERE, null, ex);
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_Codigo = new javax.swing.JLabel();
        jLabel_Descricao = new javax.swing.JLabel();
        jTextField_Descricao = new javax.swing.JTextField();
        jCheckBox_Status = new javax.swing.JCheckBox();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jLabel_Mensagem = new javax.swing.JLabel();
        jComboBox_SubSetor = new javax.swing.JComboBox();
        id_nivel = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setTitle("Nivel");
        setResizable(false);

        jLabel_Codigo.setText("Codigo :");

        jLabel_Descricao.setText("Nome :");

        jCheckBox_Status.setSelected(true);
        jCheckBox_Status.setText("Ativo");

        jButton_Ok.setText("Ok");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            List<SubSetor> listSubSetor = objFachadaBDR.listarSubSetorAtivos();
            jComboBox_SubSetor.addItem(new KeyValue("--- Favor Selecionar ---", "0"));
            for (SubSetor objSubSetor : listSubSetor) {
                jComboBox_SubSetor.addItem(new KeyValue(objSubSetor.getDescricaoSubSetor(), Integer.toString(objSubSetor.getIdSubSetor())));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        jComboBox_SubSetor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_SubSetorActionPerformed(evt);
            }
        });

        id_nivel.setText("0");
        id_nivel.enable(false);

        jLabel1.setText("Sub Setor:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_Descricao)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_Codigo)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField_Descricao, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(id_nivel, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 165, Short.MAX_VALUE)
                                .addComponent(jCheckBox_Status))
                            .addComponent(jComboBox_SubSetor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(33, 33, 33))
                    .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Codigo)
                    .addComponent(id_nivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox_Status))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jComboBox_SubSetor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Descricao)
                    .addComponent(jTextField_Descricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Ok)
                    .addComponent(jButton_Cancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:

        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {

            dispose();

        } else {
        }




    }//GEN-LAST:event_jButton_CancelarActionPerformed

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:

        int idNivel = Integer.parseInt(id_nivel.getText());
        String nome = jTextField_Descricao.getText();
        Boolean status = jCheckBox_Status.isSelected();
        int idSubSetor = Integer.parseInt(((KeyValue) jComboBox_SubSetor.getSelectedItem()).getValue());
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            SubSetor objSubSetor = objFachadaBDR.procurarSubSetor(idSubSetor);
            Nivel nivel = new Nivel(idNivel, objSubSetor, nome, status);

            if (idNivel == 0) {
                objFachadaBDR.cadastrarNivel(nivel);
            } else {
                objFachadaBDR.atualizarNivel(nivel);
            }

            String acao = (idNivel == 0) ? "Cadastrado" : "Inserido";
            jLabel_Mensagem.setText("Nivel " + acao + " Com Sucesso!!!");
            jLabel_Mensagem.setForeground(Color.BLACK);

            if (idNivel == 0) {
                jTextField_Descricao.setText("");
                jCheckBox_Status.setSelected(false);
                jComboBox_SubSetor.setSelectedIndex(0);
            }

        } catch (SubSetorNaoCadastradoException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
        } catch (CamposObrigatoriosException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
        } catch (ErroAcessoRepositorioException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
        }

    }//GEN-LAST:event_jButton_OkActionPerformed

private void jComboBox_SubSetorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_SubSetorActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jComboBox_SubSetorActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadNivel().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField id_nivel;
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_Status;
    private javax.swing.JComboBox jComboBox_SubSetor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel_Codigo;
    private javax.swing.JLabel jLabel_Descricao;
    private javax.swing.JLabel jLabel_Mensagem;
    private javax.swing.JTextField jTextField_Descricao;
    // End of variables declaration//GEN-END:variables
}
